package com.permissions.dispatcher;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.app.Context;
import ohos.bundle.IBundleManager;

/**
 * 权限封装类
 */
public class PermissionUtils {

    private PermissionUtils() {
    }

    /**
     * Checks all given permissions have been granted.
     *
     * @param grantResults results
     * @return returns true if all permissions have been granted.
     */
    public static boolean verifyPermissions(int... grantResults) {
        if (grantResults.length == 0) {
            return false;
        }
        for (int result : grantResults) {
            if (result != IBundleManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }


    /**
     * Returns true if the Activity or Fragment has access to all given permissions.
     *
     * @param context     context
     * @param permissions permission list
     * @return returns true if the Activity or Fragment has access to all given permissions.
     */
    public static boolean hasSelfPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (!hasSelfPermission(context, permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Determine context has access to the given permission.
     * <p>
     * This is a workaround for RuntimeException of Parcel#readException.
     * For more detail, check this issue https://github.com/hotchemi/PermissionsDispatcher/issues/107
     *
     * @param context    context
     * @param permission permission
     * @return true if context has access to the given permission, false otherwise.
     * @see #hasSelfPermissions(Context, String...)
     */
    private static boolean hasSelfPermission(Context context, String permission) {
        try {
            return context.verifySelfPermission(permission) == IBundleManager.PERMISSION_GRANTED;
        } catch (RuntimeException t) {
            return false;
        }
    }

    /**
     * Checks given permissions are needed to show rationale.
     *
     * @param ability    ability
     * @param permissions permission list
     * @return returns true if one of the permission is needed to show rationale.
     */
    public static boolean shouldShowRequestPermissionRationale(Ability ability, String... permissions) {
        for (String permission : permissions) {
            if (ability.canRequestPermission(permission)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Checks given permissions are needed to show rationale.
     *
     * @param abilitySlice    abilitySlice
     * @param permissions permission list
     * @return returns true if one of the permission is needed to show rationale.
     */
    public static boolean shouldShowRequestPermissionRationale(AbilitySlice abilitySlice, String... permissions) {
        for (String permission : permissions) {
            if (abilitySlice.canRequestPermission(permission)) {
                return true;
            }
        }
        return false;
    }


    /**
     * request permissions
     * @param ability use to get method
     * @param permissions permissions
     * @param requestCode code
     */
    public static void requestPermissionsFromUser(Ability ability,String[] permissions, int requestCode){
        ability.requestPermissionsFromUser(permissions,requestCode);
    }

    /**
     * request permissions
     * @param slice use to get method
     * @param permissions permissions
     * @param requestCode code
     */
    public static void requestPermissionsFromUser(AbilitySlice slice,String[] permissions, int requestCode){
        slice.requestPermissionsFromUser(permissions,requestCode);
    }
}
